package top.xxyu.file.service;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import javax.servlet.ServletContext;

import org.apache.shiro.web.config.IniFilterChainResolverFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.SpringApplication;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.ServletContextResource;
import org.springframework.web.context.support.ServletContextResourceLoader;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.support.RequestPartServletServerHttpRequest;

import top.xxyu.file.service.impl.FileServiceImpl;
import ztest.MybatisApp;

public class FileUtil {
	
	public static final String REAL_NAME = "realName";
            
	public static final String SUFFIX_NAME = "suffixName";
            
	public static final String FILE_UUID_NAME ="fileUuidName";
            
	public static final String FILE = "file";
	
	public static final  String FILE_PATH ="filePath";
	
	private static Logger log = LoggerFactory.getLogger(FileUtil.class);

	
	public static Map<String, Object> getFileFromMultiparFile(MultipartFile multiparFile) {
		Map<String, Object> map = new HashMap<>();
		// 获取文件名
		String realName = multiparFile.getOriginalFilename();
		log.info("上传的文件名为：" + realName);
		map.put(FileUtil.REAL_NAME, realName);

		// 获取文件的后缀名
		String suffixName = realName.substring(realName.lastIndexOf("."));
		log.info("上传文件的后缀名为：" + suffixName);
		map.put(FileUtil.SUFFIX_NAME, suffixName);
		
		// 文件上传后文件夹的路径
//		String fileParentPath = "c://file";
		String fileParentPath = FileUtil.class.getClassLoader().getResource("").getFile()+"static/upload";
		// 解决中文问题，linux下路径，图片显示问题
		String fileUuidName = UUID.randomUUID() + suffixName;
		map.put(FileUtil.FILE_UUID_NAME, fileUuidName);
		// 文件路径
		String filePath = fileParentPath + "/" + fileUuidName;
		map.put(FileUtil.FILE_PATH, filePath);
		

		File file = new File(filePath);
		// 检测是否存在目录
		if (!file.getParentFile().exists()) {
			file.getParentFile().mkdir();
		}
		map.put(FileUtil.FILE, file);
		return map;
	}
}
